package com.example.rabbitmq.topic;

import com.example.rabbitmq.constant.Constants;
import com.rabbitmq.client.BuiltinExchangeType;
import com.rabbitmq.client.Channel;
import com.rabbitmq.client.Connection;
import com.rabbitmq.client.ConnectionFactory;

import java.io.IOException;
import java.util.concurrent.TimeoutException;

public class Producer {
    public static void main(String[] args) throws IOException, TimeoutException {
        ConnectionFactory factory=new ConnectionFactory();
        factory.setHost(Constants.HOST);
        factory.setPort(Constants.PORT);
        factory.setUsername(Constants.USERNAME);
        factory.setPassword(Constants.PASSWORD);
        factory.setVirtualHost(Constants.VIRTUALHOST);
        //创建连接
        Connection connection = factory.newConnection();
        //创建通道
        Channel channel = connection.createChannel();
        //声明交换机
        channel.exchangeDeclare(Constants.TOPIC_EXCHANGE, BuiltinExchangeType.TOPIC, true);
        //声明队列
        channel.queueDeclare(Constants.TOPIC_QUEUE1, true, false, false, null);
        channel.queueDeclare(Constants.TOPIC_QUEUE2, true, false, false, null);
        //绑定队列和交换机
        channel.queueBind(Constants.TOPIC_QUEUE1,Constants.TOPIC_EXCHANGE,"topic.#");
        channel.queueBind(Constants.TOPIC_QUEUE1,Constants.TOPIC_EXCHANGE,"topic.*.red");
        channel.queueBind(Constants.TOPIC_QUEUE2,Constants.TOPIC_EXCHANGE,"topic.*.green");
        channel.queueBind(Constants.TOPIC_QUEUE2,Constants.TOPIC_EXCHANGE,"topic.*");
        //发送消息
        channel.basicPublish(Constants.TOPIC_EXCHANGE,"topic.red",null,"topic.red".getBytes());
        channel.basicPublish(Constants.TOPIC_EXCHANGE,"topic.green",null,"topic.green".getBytes());
        channel.basicPublish(Constants.TOPIC_EXCHANGE,"topic.red.yellow",null,"topic.red.yellow".getBytes());
        channel.basicPublish(Constants.TOPIC_EXCHANGE,"topic.red.green",null,"topic.red.green".getBytes());
        channel.basicPublish(Constants.TOPIC_EXCHANGE,"topic.red.green",null,"topic...green".getBytes());



    }
}
